<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理 -->
      <el-button type="danger" size="mini" @click="showDialog=true">添加角色</el-button>
      <el-table :data="tableData" border style="margin:20px 0" size="mini">
        <el-table-column prop="name" label="角色" width="200">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.name }}</span>
            <el-input v-else v-model="row.editRow.name" />
          </template>
        </el-table-column>
        <el-table-column label="是否启用" width="200">
          <template #default="{ row }">
            <span v-if="!row.isEdit">{{ row.state ? "启用" : "未启用" }}</span>
            <el-switch v-else v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述">
          <template #default="{row}">
            <!-- <el-input v-if="!row.isEdit" v-model="row.editRow.description" size="mini" type="textarea" /> -->
            <span v-if="!row.isEdit">{{ row.description }}</span>
            <el-input v-else v-model="row.editRow.description" type="textarea" size="mini" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <!-- {{ row }} -->
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="editOk(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <!-- 非编辑状态 -->
              <el-button type="text">分配权限</el-button>
              <el-button type="text" @click="edit(row)">编辑</el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="del(row.id)">
                <el-button slot="reference" style="margin-left:10px" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        small
        :current-page="page"
        :page-sizes="[3,4,5,6]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
      <!-- 新增角色对话框 -->
      <el-dialog title="新增角色" :visible.sync="showDialog" width="500px" @close="clear">
        <template #title>新增</template>
        <el-form ref="xx" :model="formObj" :rules="rules" label-width="100px" size="mini">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formObj.name" />
          </el-form-item>
          <el-form-item label="启用">
            <el-switch
              v-model="formObj.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formObj.description" type="textarea" />
          </el-form-item>
        </el-form>
        <template>
          <el-button size="mini" type="primary" @click="add">确定</el-button>
          <el-button size="mini" @click="clear">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addRole, deleteRole, getRoleList, updateRole } from '@/api/role'

export default {
  name: 'Role',
  data() {
    return {
      tableData: [
        // {
        //   date: '2016-05-02',
        //   id: 1,
        //   name: '王小虎',
        //   address: '上海市普陀区金沙江路 1518 弄'
        // },
        // {
        //   date: '2016-05-04',
        //   id: 2,
        //   name: '王中虎',
        //   address: '上海市普陀区金沙江路 1517 弄'
        // },
        // {
        //   date: '2016-05-01',
        //   id: 3,
        //   name: '王大虎',
        //   address: '上海市普陀区金沙江路 1519 弄'
        // },
        // {
        //   date: '2016-05-03',
        //   id: 4,
        //   name: '王老虎',
        //   address: '上海市普陀区金沙江路 1516 弄'
        // }
      ],
      total: 0,
      page: 1, // 当前页码
      pagesize: 5, // 每页显示条数
      showDialog: false,
      isEdit: false,
      // 表单对象数据
      formObj: {
        name: '',
        description: '',
        state: 1 // 1表示启用 0表示禁用
      },
      rules: {
        name: [
          // 必填校验
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          // 必填校验
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async editOk(row) {
      if (row.editRow.name && row.editRow.description) {
        await updateRole({ ...row.editRow, id: row.id })
        this.$message.success('修改角色成功')
        Object.assign(row, {
          ...row.editRow,
          isEdit: false // 退出编辑模式
        }) // 规避eslint的误判
      } else {
        this.$message.warning('请输入角色名称和描述')
      }
    },
    edit(row) {
      row.isEdit = true // 改变行的编辑状态
      // 更新缓存数据
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    add() {
      this.$refs.xx.validate(async isOk => {
        if (isOk) {
          await addRole(this.formObj)
          this.$message.success('添加角色成功')
          this.clear()
          this.getList()
        }
      })
    },
    async  del(id) {
      await deleteRole(id)
      this.$message.success('删除角色成功')
      if (this.tableData.length === 1 && this.page > 1) {
        this.page--
      }
      this.getList()
    },
    clear() {
      this.showDialog = false
      this.$refs.xx.resetFields()
      this.formObj = {
        name: '',
        description: '',
        state: 1
      }
    },
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.getList()
    },
    async getList() {
      const { rows, total } = await getRoleList(this.page, this.pagesize)
      this.tableData = rows
      this.total = total
      this.tableData.forEach(item => {
        // item.isEdit = false  //添加一个属性   初始值为false
        // 数据响应式的问题  数据变化 视图更新
        // 添加的动态属性 不具备响应式特点
        // this.$set(目标对象, 属性名称, 初始值) 可以针对目标对象 添加的属性 添加响应式
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    }
  }
}
</script>
